<?php

/**
 * Created by PhpStorm.
 * User: Weizehua
 * Date: 8/7/2016
 * Time: 10:58
 */
class Test
{
    //$evaluatorPath = $_REQUEST['evaluator'];
    //$code = $_REQUEST['code'];
    public $evaluatorPath = 'http://119.29.159.124:81/client/evaluator_rsa.php';
    
    function evaluation($auth, $code)
    {
        // prepare rsa encryption
        $privateKeyStr = <<<key
-----BEGIN PRIVATE KEY-----
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAOPI+6PH3g8uZMd0
KXJ9WaOCC02JTHNpdMgdBMwlAat/UKJqw9iHMwj16v+ftVC9ZeRynrGRLbDeAI2A
iRpfEi0yy4naeFgd5+qQnEgdRBXF7eWzGEHRvhg6+gXIZr/By+LXRVrcSD8Wi7+T
riB30N406Wu5L4YoXYQ8e4u24dYfAgMBAAECgYEAicaPLkk970DIhDumtjFLfrKx
03wwWb53fHLQ7AmWMP0Y8KRTQSzFma3lLiiTqvpkHwGVKyH82PXur6gHRYQroTbV
qMFvUIoh0V2BjA5u08FdH0jvgB13+Bq1bFmUrFUG2XudCPBP888+TBJzuLco00TT
wmX96Jrvh5poNafsR4ECQQD4CZKlCHcKdROeVeZLizhl49R5Ca7ZXqOa0CUdLgK5
nuxTFwENfHO1WVXN8dpI+2XFVAj6aV0tCRJaEQmfHwyXAkEA6xj43iymeoFIgqjl
qxwZW2JBVuTSbzOoyjwfFilbEGvmiTO/CWDfe4hPhZGhTSa5SmQaMj5KTNs47+bd
ifrLuQJAd/HJJF1vk3MCRbMsZqOwZjVCbjWshBkr7JVzHSU+1BxyE8wwCiGAotXM
lIa8FEbd9AqrPMzu7fRRF5TdnkEZiQJAEQ6vx9bvgUXuD+TXPRH92P6JBRf3GLBj
18CC9OWtQMTDZkMF4lVQAbODKYrGbEWZ/gbDS8/vLiJdwDI88qzqqQJAcaVoGKW1
19XJvBGq8gw5D99Uokba2EGAybco/ngq8m7RvcEn2pbd6tgcC10MfVHc5uGjUsTm
0AUP7vXU4diKDQ==
-----END PRIVATE KEY-----
key;
        $privateKey = openssl_get_privatekey($privateKeyStr);
        
        // prepare params
        $encryptedAuth = "";
        assert(openssl_private_encrypt($auth, $encryptedAuth, $privateKey));
        $passingAuth = base64_encode($encryptedAuth);
        
        $passingCode = $code;
        $data = $code . $auth;
        $passingHash = hash_hmac('md5', $data, $auth);
        $passingMethod = 'eval';
        
        // construct request
        $params = array(
            'action' => 'eval',
            'auth' => $passingAuth,
            'code' => $passingCode,
            'hash' => $passingHash,
            'method' => $passingMethod
        );
        $opt = array('http' => array(
            'method' => 'POST',
            'header' => 'Content-Type: application/x-www-form-urlencoded',
            'content' => http_build_query($params)
        ));
        
        // post
        $data = file_get_contents($this->evaluatorPath, false, stream_context_create($opt));
        return $data;
    }
    
    
    function getAuth()
    {
        // construct request
        $params = array(
            'action' => 'authCode'
        );
        $opt = array('http' => array(
            'method' => 'POST',
            'header' => 'Content-Type: application/x-www-form-urlencoded',
            'content' => http_build_query($params)
        ));
        
        // get auth code
        $data = file_get_contents($this->evaluatorPath, false, stream_context_create($opt));
        if ($data)
        {
            $obj = json_decode($data);
            if ($obj)
            {
                $auth = $obj->authCode;
                return $auth;
            }
        }
        return false;
    }
}

$flag = md5(strval(time()) . 'rsa_eval_test_succeed');
$code = "echo '$flag';";
$t = new Test();
$data = $t->evaluation($t->getAuth(), $code);
if(strstr($data, $flag))
{
    echo 'test passed';
}
else
{
    echo "test failed, response : <br>";
    echo $data;
}
